package walker

import (
	"io"
	"log"
	"os"
)

// 同时输出到屏幕 && log文件
type logWriter struct {
	// f      *os.File
	stdout io.Writer
}

func (w *logWriter) Write(bs []byte) (int, error) {
	return w.stdout.Write(bs)
	// return w.f.Write(bs) // todo 打开 log
}

func (w *logWriter) Close() error {
	// w.f.Sync()
	// return w.f.Close()

	return nil
}

func NewLog(p string) (*logWriter, error) {
	// f, err := os.Create(p)
	// if err != nil {
	// 	return nil, err
	// }

	return &logWriter{
		// f:      f,
		stdout: os.Stdout,
	}, nil
}

var logWriters []*logWriter

func initLog(p string) {
	// log.
	w, err := NewLog(p)
	if err != nil {
		panic("init log err")
	}
	logWriters = append(logWriters, w)
	log.SetOutput(w)
}

// func logln(s string, a ...any) {
// 	log.Println(s2)
// }

func finishLogging() {

	for _, l := range logWriters {
		l.Close()
	}
}
